/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import java.io.Serializable;
import java.util.Set;

import org.gwt.mosaic.ui.client.ListBox;
import org.gwt.mosaic.ui.client.ListBox.CellRenderer;
import org.gwt.mosaic.ui.client.list.DefaultListModel;

import com.google.gwt.gen2.table.event.client.RowSelectionEvent;
import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
import com.google.gwt.gen2.table.event.client.TableEvent.Row;
import com.google.gwt.user.client.ui.Widget;

/**
 * This element is used to create a list of items where one or more of the items
 * may be selected. A listbox may contain multiple columns. There are numerous
 * methods which allow the items in the listbox to be retrived and modified.
 * <p>
 * A listbox is expected to contain listitem elements.
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Listbox extends Container {
  private static final long serialVersionUID = 5145334120463808387L;

  private transient ListBox<Listitem> listbox;

  private Listcols listcols;

  private DefaultListModel<Listitem> listitems = new DefaultListModel<Listitem>();

  public Listbox() {
    super();
  }

  public Object getSelectedElement() {
    if (listbox != null) {
      int index = listbox.getSelectedIndex();

      if (index == -1) {
        return null;
      }

      return listbox.getItem(index);
    }

    return null;
  }

  public void setSelectedElement(Object element) {
  }

  @Override
  public Element add(Element element, int index) {
    if (element instanceof Listcols) {
      if (this.listcols != null) {
        remove(this.listcols);
      }
      this.listcols = (Listcols) element;
      return super.add(element, index);
    } else if (element instanceof Listitem) {
      this.listitems.add((Listitem) element);
      return super.add(element, index);
    }
    return null;
  }

  @Override
  public Element remove(int index) {
    Element element = elements.get(index);
    if (element instanceof Listcols) {
      this.listcols = null;
      return super.remove(index);
    } else if (element instanceof Listitem) {
      this.listitems.remove(index);
      return super.remove(index);
    }
    return null;
  }

  @Override
  protected Widget createUI() {
    listbox = new ListBox<Listitem>();
    listbox.setCellRenderer(new CellRenderer<Listitem>() {
      public void renderCell(ListBox<Listitem> listBox, int row, int column,
          Listitem item) {
        for (Element element : item.elements) {
          Listcell listcell = (Listcell) element;
          listBox.setText(row, column, listcell.getLabel());
        }
      }
    });
    listbox.setModel(listitems);
    listbox.addRowSelectionHandler(new RowSelectionHandler() {
      public void onRowSelection(RowSelectionEvent event) {
        Set<Row> selection = event.getNewValue();
        if (selection != null && selection.size() > 0) {
          attributeMap.put(
              "selectedElement",
              (Serializable) listbox.getItem(selection.iterator().next().getRowIndex()));
        } else {
          attributeMap.put("selectedElement", (Serializable) null);
        }
      }
    });

    return listbox;
  }

}
